#!/bin/bash

# Kubernetes一键安装脚本

# 检查操作系统类型
detect_os() {
    if [ -f /etc/ky10-release ]; then
        echo "ky10"
    elif [ -f /etc/lsb-release ]; then
        echo "ubuntu"
    else
        echo "unknown"
    fi
}

# 检查是否为root用户
if [ "$(id -u)" != "0" ]; then
   echo "请使用root用户运行此脚本"
   exit 1
fi

# 安装Docker
install_docker() {
    echo "正在安装Docker..."
    os_type=$(detect_os)
    
    if [ "$os_type" = "ky10" ]; then
        yum install -y yum-utils
        yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
        yum install -y docker-ce docker-ce-cli containerd.io
    else
        apt-get update
        apt-get install -y apt-transport-https ca-certificates curl software-properties-common
        curl -fsSL https://download.docker.com/linux/ubuntu/gpg | apt-key add -
        add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
        apt-get update
        apt-get install -y docker-ce
    fi
    
    systemctl enable docker
    systemctl start docker
    echo "Docker安装完成"
}

# 安装Kubernetes组件
install_k8s() {
    echo "正在安装Kubernetes组件..."
    os_type=$(detect_os)
    
    if [ "$offline_mode" = "true" ]; then
        echo "离线模式安装..."
        if [ "$os_type" = "ky10" ]; then
            yum localinstall -y /opt/k8s-offline/*.rpm
            systemctl enable --now kubelet
        else
            dpkg -i /opt/k8s-offline/*.deb
            apt-mark hold kubelet kubeadm kubectl
        fi
    else
        if [ "$os_type" = "ky10" ]; then
            cat <<EOF > /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://packages.cloud.google.com/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://packages.cloud.google.com/yum/doc/yum-key.gpg https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg
EOF
            yum install -y kubelet kubeadm kubectl
            systemctl enable --now kubelet
        else
            apt-get update && apt-get install -y apt-transport-https curl
            curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key add -
            cat <<EOF >/etc/apt/sources.list.d/kubernetes.list
deb https://apt.kubernetes.io/ kubernetes-xenial main
EOF
            apt-get update
            apt-get install -y kubelet kubeadm kubectl
            apt-mark hold kubelet kubeadm kubectl
        fi
    fi
    
    echo "Kubernetes组件安装完成"
}

# 配置系统参数
configure_system() {
    echo "正在配置系统参数..."
    # 关闭swap
    swapoff -a
    sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab
    
    # 配置内核参数
    cat <<EOF > /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
    sysctl --system
    
    echo "系统参数配置完成"
}

# 初始化Kubernetes集群
init_cluster() {
    echo "正在初始化Kubernetes集群..."
    kubeadm init --pod-network-cidr=10.244.0.0/16
    
    # 配置kubectl
    mkdir -p $HOME/.kube
    cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
    chown $(id -u):$(id -g) $HOME/.kube/config
    
    # 安装网络插件
    if [ "$offline_mode" = "true" ]; then
        kubectl apply -f /opt/k8s-offline/kube-flannel.yml
    else
        kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
    fi
    
    echo "Kubernetes集群初始化完成"
}

# 获取join命令
get_join_command() {
    echo "请运行以下命令将节点加入集群:"
    kubeadm token create --print-join-command
}

# Node节点加入集群
join_cluster() {
    echo "请输入master节点生成的join命令:"
    read join_cmd
    eval $join_cmd
    echo "节点已成功加入集群"
}

# 配置SSH免密登录
setup_ssh() {
    echo "正在配置SSH免密登录..."
    if [ ! -f ~/.ssh/id_rsa ]; then
        ssh-keygen -t rsa -N "" -f ~/.ssh/id_rsa
    fi
    
    if [ -n "$node_ip" ]; then
        ssh-copy-id -i ~/.ssh/id_rsa.pub root@$node_ip
    fi
    echo "SSH免密登录配置完成"
}

# 主执行流程
main() {
    # 加载配置文件
    if [ ! -f "config.cfg" ]; then
        echo "错误: 找不到配置文件config.cfg"
        exit 1
    fi
    source config.cfg
    
    # 检查是否离线模式
    if [ -z "$offline_mode" ]; then
        offline_mode="false"
    fi
    
    # 验证必要参数
    if [ -z "$master_ip" ]; then
        echo "错误: 配置文件中未指定master_ip参数"
        exit 1
    fi
    
    if [ ${#node_ips[@]} -eq 0 ]; then
        echo "警告: 未配置任何Node节点IP"
    fi
    
    install_docker
    install_k8s
    configure_system
    
    # 初始化Master节点
    init_cluster
    join_cmd=$(kubeadm token create --print-join-command)
    
    # 循环处理所有Node节点
    for node_ip in "${node_ips[@]}"; do
        setup_ssh
        echo "正在在节点 $node_ip 上安装必要组件..."
        ssh root@$node_ip "$(declare -f install_docker install_k8s configure_system); install_docker; install_k8s; configure_system"
        echo "正在将join命令发送到节点 $node_ip..."
        ssh root@$node_ip "$join_cmd"
        echo "节点 $node_ip 已成功加入集群"
    done
    
    echo "Kubernetes安装完成!"
    echo "运行以下命令使非root用户可以使用kubectl:"
    echo "  mkdir -p $HOME/.kube"
    echo "  sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config"
    echo "  sudo chown $(id -u):$(id -g) $HOME/.kube/config"
}

# 执行主函数
main